package com.lyn.solitude_plugin;


import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import com.lyn.solitude_aidl_host.PluginHostAidlInterface;
import com.lyn.solitude_plugin_api.entity.Plugin;
import com.lyn.solitude_plugin_api.plugin.PluginActivityLifecycle;


/**
 * 插件activity
 *
 * @author longyn 2024/4/20
 * @version 1.0.0
 */
public class PluginActivityActivity implements PluginActivityLifecycle {

    public final String TAG = getClass().getSimpleName();

    private final Handler handler = new Handler();
    private AppCompatActivity activity;
    private Resources originalResources;
    private Plugin plugin;
    private PluginHostAidlInterface.Stub stub;


    @Override
    public void onPlugin(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public Plugin getPlugin() {
        return this.plugin;
    }

    @Override
    public void onPluginHostAidlInterfaceStub(PluginHostAidlInterface.Stub stub) {
        this.stub = stub;
    }

    @Override
    public PluginHostAidlInterface.Stub getPluginHostAidlInterfaceStub() {
        return this.stub;
    }

    @Override
    public AppCompatActivity getActivity() {
        return this.activity;
    }

    @Override
    public Context getContext() {
        return activity;
    }

    @Override
    public void onActivity(AppCompatActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void setContentView(int layoutId) {
        activity.setContentView(layoutId);
    }

    @Override
    public void setContentView(View contentView) {
        activity.setContentView(contentView);
    }

    @Override
    public Intent getIntent() {
        return activity.getIntent();
    }

    @Override
    public AssetManager getAssets() {
        return activity.getAssets();
    }

    @Override
    public ClassLoader getClassLoader() {
        return activity.getClassLoader();
    }

    @Override
    public Resources getResources() {
        return activity.getResources();
    }

    @Override
    public String getString(int resId) {
        return activity.getString(resId);
    }

    @Override
    public int getColor(int resId) {
        return activity.getColor(resId);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public Drawable getDrawable(int redId) {
        return activity.getDrawable(redId);
    }

    @Override
    public LayoutInflater getLayoutInflater() {
        return activity.getLayoutInflater();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return activity.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
        return activity.onMenuOpened(featureId, menu);
    }

    @Override
    public void onNewIntent(Intent intent) {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onRestart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public boolean isNotBackPressed() {
        return false;
    }

    @Override
    public void onBackPressed() {
        activity.onBackPressed();
    }

    @Override
    public android.app.FragmentManager getFragmentManager() {
        return activity.getFragmentManager();
    }

    @Override
    public FragmentManager getSupportFragmentManager() {
        return activity.getSupportFragmentManager();
    }

    @Override
    public Application getApplication() {
        return activity.getApplication();
    }

    @Override
    public void startPluginActivity(Class<? extends PluginActivityLifecycle> pluginClass) {
        try {
            stub.jump(plugin.id,pluginClass.getName());
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        activity.startActivity(intent);
    }

    @Override
    public void startActivity(Intent intent, Bundle options) {
        activity.startActivity(intent,options);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        activity.startActivityForResult(intent,requestCode);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        activity.startActivityForResult(intent,requestCode,options);
    }

    @Override
    public void onOriginalResources(Resources resources) {
        this.originalResources = resources;
        if (resources!=null){
            Log.d(TAG,"onOriginalResources not null");
        }else {
            Log.d(TAG,"onOriginalResources is null");
        }
    }

    @Override
    public Resources getOriginalResources() {
        activity.getResources();
        return this.originalResources;
    }

    @Override
    public ActionBar getSupportActionBar() {
        return this.activity.getSupportActionBar();
    }

    @Override
    public void finish() {
        activity.finish();
    }

    @Override
    public <T extends View> T findViewById(@IdRes int id) {
        return activity.findViewById(id);
    }

    @Override
    public Window getWindow() {
        return activity.getWindow();
    }

    @Override
    public <T> T getSystemService(Class<T> serviceClass) {
        return activity.getSystemService(serviceClass);
    }

    @Override
    public Object getSystemService(String name) {
        return activity.getSystemService(name);
    }

    @Override
    public MenuInflater getMenuInflater() {
        return activity.getMenuInflater();
    }

    @Override
    public Lifecycle getLifecycle() {
        return activity.getLifecycle();
    }

    public Handler getHandler() {
        return handler;
    }

}
